package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.CfgDictionaryEntity;
import com.baitengsoft.patentapimonitor.api.model.AdminDataauditViewModel;
import com.baitengsoft.patentapimonitor.api.model.param.AdminDataauditParam;
import com.baitengsoft.patentapimonitor.api.model.param.EditAdminDataauditParam;
import com.baitengsoft.patentapimonitor.api.service.AdminDataauditService;
import com.baitengsoft.patentapimonitor.api.service.CfgDictionaryService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.TemplateFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Admin on 2017/12/29.
 */

@RestController
@RequestMapping("api/v1/admin")
@Api(value = "后台-数据稽核",description = "数据稽核")
public class AdminDataAuditController extends AbstractController {

    @Autowired
    private  AdminDataauditService adminDataauditService;
    @Autowired
    private CfgDictionaryService cfgDictionaryService;

    @ApiOperation(value = "获取稽核方式")
    @RequestMapping(value="/getauditmethod", method= RequestMethod.GET)
    public Res getAuditMethod(){

        Map<String,Object> map=new HashMap<>();

        Boolean flag=false;
        try {
            List<CfgDictionaryEntity> cfgDictionaryEntities=cfgDictionaryService.searchDictionaryByCode("1002");
            if(cfgDictionaryEntities!=null){
                flag=true;
                map.put("data",cfgDictionaryEntities);
            }else {
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "获取稽核数据列表")
    @RequestMapping(value="/searchdataaudit", method= RequestMethod.POST)
    public Res searchDataAudit(@RequestBody AdminDataauditParam param){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            PageResult<AdminDataauditViewModel> pageResult= adminDataauditService.searchDataaudit(param);
            if(pageResult!=null){
                flag=true;
                map.put("data",pageResult);
            }else {
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "获取稽核数据")
    @RequestMapping(value="/searchdataauditbyid", method= RequestMethod.GET)
    public Res searchDataAuditById(@RequestParam("id") int id){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            AdminDataauditViewModel viewModel= adminDataauditService.searchById(id);
            if(viewModel!=null){
                flag=true;
                map.put("data",viewModel);
            }else {
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "编辑稽核数据")
    @RequestMapping(value="/editdataaudit", method= RequestMethod.POST)
    public Res editDataAudit(@RequestBody EditAdminDataauditParam param){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer successId= adminDataauditService.editDataaudit(param,getAdminUserId());
            if(successId==1){
                flag=true;
                map.put("msg","操作成功");
            }else {
                map.put("msg","操作失败");
            }
        } catch (Exception e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "删除稽核数据")
    @RequestMapping(value="/deldataauditbyids", method= RequestMethod.GET)
    public Res delDataAuditByIds(@RequestParam("ids") String ids){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer successId= adminDataauditService.delDataauditByIds(ids,getAdminUserId());
            if(successId==1){
                flag=true;
                map.put("msg","删除成功");
            }else {
                map.put("msg","删除失败");
            }
        } catch (Exception e) {
            map.put("msg","删除异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }





    /**
     * 下载模板
     * @param request
     * @param response
     * @return
     */
//    @ApiOperation(value = "下载专利模板")
//    @RequestMapping(value="/searchu", method= RequestMethod.GET)
//    public Res searchU(HttpServletRequest request, HttpServletResponse response){
//
//        try {
//            String fileName="专利模板.xls";
//            fileName=new String(fileName.getBytes("gb2312"), "iso8859-1");
//            //String filePath = request.getSession().getServletContext().getRealPath("/")+"resources/excel-templates/"+fileName;
//            response.setContentType("multipart/form-data");
//            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
//            //InputStream inputStream = new FileInputStream(filePath);
//            InputStream inputStream= TemplateFileUtil.getTemplates("PatentTemplate.xls");
//            Workbook workbook = new HSSFWorkbook(inputStream);
//            OutputStream outputStream = response.getOutputStream();
//            inputStream.close();
//            //在这一步之前可以对workbook赋值，它只是一个excel文件对象，可以用代码赋值写入你想要的excel数据
//            workbook.write(outputStream);
//            outputStream.close();
//            outputStream.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return Res.ok();
//    }


}
